(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.currency.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'currencyService', 'commonService'];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, currencyService, commonService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("currencyList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			
			currencyService.getCountryOptions().then(function(response) {
				response.hasEmptyOption = true;
				ctrl.countryCodeOptions = response;
			}, function(error) {
				log.debug('can\'t load country code options' + angular.toJson(error));
			});
			currencyService.getIntCalcBasisOptions().then(function(response) {
				response.hasEmptyOption = true;
				ctrl.calbaseOptions = response;
			}, function(error) {
				log.debug('can\'t load interest calc options' + angular.toJson(error));
			});	
			
			currencyService.getDataStatus().then(function(response) {
				ctrl.dataStatusOptions = response;
			}, angular.noop);
			
			ctrl.searchCurrency();
		};
		
		function getDecimalNumOption() {
			var option = {
					"data" : [
					 {
						"value" : "",
						"label" : ""
					 },
					 {
						"value" : "1",
						"label" : "1"
					}, {
						"value" : "2",
						"label" : "2"
					}, {
						"value" : "3",
						"label" : "3"
					} ]
			};
			return option;
		}

		ctrl.viewCurrency = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.currency.view', ctrl.selectedRows[0]);
		};

		ctrl.createCurrency = function() {
			$scope.go('cda.currency.create');
		};

		ctrl.updateCurrency = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.currency.update', ctrl.selectedRows[0]);
		};

		ctrl.deleteCurrency = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.currency.delete', ctrl.selectedRows[0]);
		};

		ctrl.searchCurrency = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.searchCurrency();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.searchCurrency();
		}
	}
})(angular);